//
//  AFNetworkTool.m
//  test
//
//  Created by 朱圳 on 2018/1/9.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "AFNetworkTool.h"
#import "EasyShowView.h"
static AFHTTPSessionManager*manager;
static AFNetworkTool *netWorkTool;
@implementation AFNetworkTool

+(AFNetworkTool *)shareManager{
    static dispatch_once_t once;
    dispatch_once(&once,^{
        if (netWorkTool==nil) {
            netWorkTool = [[AFNetworkTool alloc]init];
            manager = [AFHTTPSessionManager manager];
            [manager setResponseSerializer:[AFJSONResponseSerializer new]];
            [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
            manager.requestSerializer.timeoutInterval = 30.0f;
            [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
            [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/xml",@"text/css",@"text/plain",@"form-data",@"x-www-form-urlencoded",@"application/x-www-form-urlencoded",@"image/jpeg",@"image/png", nil]];
        }
    });
    return  netWorkTool;
}

/**
 *   get 请求
 *
 *  @param httpUrl     请求url
 *  @param parameters   除文件外的后台要求参数
 *  @param successData 成功回调函数
 *  @param failure 失败回调函数
 */
- (void)getJSONWithUrl:(NSString * )httpUrl parame:(id)parameters  successData:(void(^)(id json))successData failure:(void (^)(NSError *error))failure{
    
    [manager GET:httpUrl parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject)
        {
            successData(responseObject);
           
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);

    }];
    
}


/**
 *  post 请求
 *
 *  @param url     请求url
 *  @param parameters   除文件外的后台要求参数
 *  @param successData 成功回调函数
 *  @param failure 失败回调函数
 */
- (void)postJSONWithUrl : (NSString *)url parmas : (id)parameters successData:(CallBack)successData failure:(void (^)(NSError *error))failure
{
//    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
//    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
//    [EasyShowLodingView showLodingText:@"正在加载中"];
    [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            
            successData(responseObject);
//            [EasyShowLodingView hidenLoding];
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
//        [EasyShowLodingView hidenLoding];
    }];
//     [EasyShowLodingView hidenLoding];

}

/**
 单张图片上传
 
 @param url 上传地址
 @param params 请求参数
 @param data 图片的data
 @param progress 进度
 @param successHandle 成功回调函数
 @param failureHandel 失败回调函数
 */
- (void)upLoadSinglePictureWithUrl:(NSString *)url
                        parameters:(id _Nullable)params
                          fileData:(NSData * _Nullable)data
                          progerss:(void (^ _Nullable)(NSProgress * _Nullable progress))progress
                           success: (void(^ _Nullable)(id _Nullable json))successHandle
                           failure:(void (^ _Nullable)(NSError * _Nullable error))failureHandel{
    
    
    if (data==nil)return;
    
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //添加需要上传的文件
        //系统当前时间
        NSDate *date=[NSDate date];
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSString *datestr=[formatter stringFromDate:date];
        NSString *filename = [NSString stringWithFormat:@"%@.jpg",datestr];
        [formData appendPartWithFileData:data name:@"files" fileName:filename mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            if (successHandle) {
                successHandle(responseObject);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureHandel) {
            failureHandel(error);
        }
        
    }];
    
    
}

@end

